﻿using AutoMapper;
using Entity.BaseInfo.DTO.WarehousePosition;
using Entity.BaseInfo.Entity;
using Entity.BaseInfo.View.WarehousePosition;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace Service.BaseInfo
{
    public class WarehousePositionService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public WarehousePositionService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取仓位列表
         */
        public async Task<RequestResponseModel<PageResult<WarehousePositionView>>> GetWarehousePositionList(WarehousePostionSearchDTO warehousePositionSearchDTO)
        {
            //筛选
            var query = _context.WarehousePositionView
                .WhereIF(a => a.Code.Contains(warehousePositionSearchDTO.Code), !string.IsNullOrEmpty(warehousePositionSearchDTO.Code))
                .WhereIF(a => a.Name.Contains(warehousePositionSearchDTO.Name), !string.IsNullOrEmpty(warehousePositionSearchDTO.Name))
                ;

            //返回列表
            var warehouseList = await query
                .OrderBy(a => a.CreateTime)
                .Page(warehousePositionSearchDTO.CurrentPage, warehousePositionSearchDTO.PageSize)
                .ToListAsync();

            //总数据量
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<WarehousePositionView>(warehouseList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增仓位
         */
        public async Task<RequestResponseModel<string>> AddWarehousePosition(WarehousePositionAddDTO warehousePositionAddDTO)
        {
            //新增数据
            var warehousePosition = _mapper.Map<WarehousePositionModel>(warehousePositionAddDTO);
            warehousePosition.CreateTime = DateTime.Now;
            warehousePosition.Id = Guid.NewGuid().ToString();

            //更新表
            _context.WarehousePosition.Add(warehousePosition);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "新增成功", warehousePosition.Id);
        }

        /**
         * 更新仓位
         */
        public async Task<RequestResponseModel<string>> PutWarehousePosition(WarehousePositionUpdateDTO warehousePositionUpdateDTO)
        {
            //校验数据
            var query = await Valid.ValidId<WarehousePositionModel>(_context.WarehousePosition, warehousePositionUpdateDTO.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", warehousePositionUpdateDTO.Id);
            }

            //编辑数据
            var warehousePosition = _mapper.Map(warehousePositionUpdateDTO, query);

            //更新表
            _context.WarehousePosition.Update(warehousePosition);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "更新成功", warehousePosition.Id);
        }

        /**
         * 删除仓位
         */
        public async Task<RequestResponseModel<string>> DeleteWarehousePosition(string Id)
        {
            //校验数据
            var query = await Valid.ValidId<WarehousePositionModel>(_context.WarehousePosition, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            //更新表
            _context.WarehousePosition.Remove(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }

        /**
         * 下拉选择仓位
         */
        public async Task<RequestResponseModel<List<WarehousePositionSelectView>>> GetWarehousePositionSelectList(WarehousePostionSelectSearchDTO warehousePositionSelectSearchDTO)
        {
            //筛选
            var query = await _context.WarehousePositionSelectView
                .WhereIF(a => a.Code.Contains(warehousePositionSelectSearchDTO.KeyWord) || a.Name.Contains(warehousePositionSelectSearchDTO.KeyWord), !string.IsNullOrEmpty(warehousePositionSelectSearchDTO.KeyWord))
                .ToListAsync();

            //返回数据
            return RequestResponse.CreateResponse(200, "查询成功", query);
        }
    }
}
